<template>
  <div class="flex-1 h-full flex flex-col p-5">
    <div  class="flex-1 h-0">
      <el-table v-loading="loading" height="100%" border ref="tableRoleRef" @select-all="selectAllCallback"
                @select="selectCallback" :data="roleData">
        <el-table-column type="selection" width="55"/>
        <el-table-column label="角色名称" header-align="center" align="center" prop="roleName">
        </el-table-column>
      </el-table>
    </div>
    <div >
      <BasePage @load="loadRole"  v-model:current="queryForm.current" v-model:size="queryForm.size"
                :total="queryForm.total"/>
    </div>
  </div>
</template>

<script setup lang="ts">
import {nextTick, onMounted, reactive, ref} from "vue";
import {listRolePage} from "/@/api/system/RoleApi";
const emit = defineEmits(['setRole','setRoleAll'])
const loading = ref(false)
const roleData = ref()
const tableRoleRef = ref()
const selectedData = ref()
const queryForm = reactive({
  total: 0,
  current: 1,
  size: 10
})
/**
 * 加载角色
 */
const loadRole = async () => {
  loading.value = true
  let res = await listRolePage(queryForm, () => {
    loading.value = false
  })
  roleData.value = res.records
  queryForm.total = res.total

}
const selectAllCallback = (selection) => {
  if (selection.length > 0) {
    emit("setRoleAll",selection,"roleId","roleName")
  } else {
    emit("setRoleAll",[],"roleId","roleName")
  }
}
const selectCallback = (selection, row) => {
  //查找列表中是否有当前行，判断当前操作是否为选中
  let index = selection.findIndex(res => res.roleId == row.roleId)
  if (index != -1) {
    emit("setRole",'add',row,"roleId","roleName")
  } else {
    emit("setRole",'delete',row,"roleId","roleName")
  }
}
/**
 * 设置选中
 * @param roleIds
 */
const setSelectedRow = (roleIds) => {
  tableRoleRef.value.clearSelection()
  roleData.value.forEach(res => {
    if (roleIds.includes(res.roleId)) {
      tableRoleRef.value.toggleRowSelection(res, undefined)
    }
  })
}
onMounted(()=>{
  loadRole()
})
defineExpose({setSelectedRow})
</script>

<style scoped>

</style>